package com.ls.fw.frame.data.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ls.fw.frame.data.dao.DataTypeDao;
import com.ls.fw.frame.data.vo.DataTypeVo;
import com.ls.fw.frame.data.model.DataType;
import com.ls.fw.commons.core.utils.reflect.ClassUtils;
import com.ls.fw.data.rdb.impl.dao.EntityDaoImpl;
import com.ls.fw.data.rdb.impl.mybatis.mapper.entity.Example;
import com.ls.fw.data.exception.DataException;
import com.ls.fw.frame.data.dao.mapper.DataTypeMapper;
import com.ls.fw.data.rdb.utils.ArraysUtils;
/**
 * 
 * @author ls
 * 2016年06月10日 16时40分37秒
 */
@Repository("dataTypeDao") 
public class DataTypeDaoImpl extends EntityDaoImpl<DataType,DataTypeVo,java.lang.Long> 
                            implements DataTypeDao{
 
    @Autowired
	private DataTypeMapper mapper;
	
    @Override
	public DataTypeMapper getMapper(){
	    return mapper;
    }
    
    @Override
	public List<DataType> findAll() throws DataException {
		return mapper.query(new DataTypeVo());
	}

	@Override
	public int delete(java.lang.Long id) throws DataException {
		return mapper.deleteByPrimaryKey(id);
	}

	@Override
	public Long save(DataType model) throws DataException {
		return (long) mapper.insertSelective(model);
	}

	@Override
	public int update(DataType model) throws DataException {
		return mapper.updateByPrimaryKeySelective(model);
	}

	@Override
	public DataType find(java.lang.Long id) throws DataException {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public int delete(String... id) throws DataException {
		Example example = new Example(DataType.class);
		example.or().andIn(DataType.Columns.PK_NAME,ArraysUtils.asList(id));
		return mapper.deleteByExample(example );
	}

	@Override
	public int remove(String... id) throws DataException {
	    Example example = new Example(DataType.class);
		example.or().andIn(DataType.Columns.PK_NAME,ArraysUtils.asList(id));
		DataType record = new DataType();
		record.setDeleteClass(Boolean.TRUE);
		return mapper.updateByExampleSelective(record , example);
	}

	@Override
	public List<DataType> query(DataTypeVo model)
			throws DataException {
		return this.mapper.query(model);
	}

	@Override
	public void persist(DataType model) {
		Object id = null;
		try {
			id = ClassUtils.getFieldValue(model, "id");
		} catch (SecurityException e) {
			throw new DataException(e);
		} catch (Exception e) {
			throw new DataException(e);
		}
		if(id==null){
			mapper.insertSelective(model);
		}else{
			mapper.updateByPrimaryKeySelective(model);
		}
	}
}
